//Copyright(c) Hugh Greenberg

package com.hugegreenbug.msoss;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class ImageFileHandler extends DefaultHandler {
	private WikiCommonsImage mWikiCommonsImage;
	private boolean mGotImageClass;
	private int mReadyForAuthor;
	private boolean mSmallStarted;
	private boolean mReadyForFullRes;
	private boolean mDone;
	
	public ImageFileHandler(String link) {
		super();
		mWikiCommonsImage = new WikiCommonsImage();
		mWikiCommonsImage.setInfoLink(link);
		mGotImageClass = false;
		mSmallStarted = false;
		mReadyForFullRes = false;
		mReadyForAuthor = 0;
		mDone = false;
	}
	
	@Override
	public void startDocument() {
	}
	
	@Override
	public void endDocument() {
	}
	
    @Override
	public void startElement (String uri, String name, String qName, Attributes atts) {
    	if (mDone)
    		return;
    	
    	if (!name.equals("div") && !name.equals("td") && !name.equals("a") && !name.equals("th") && 
    			!name.equals("img") && !name.equals("small") && !name.equals("p"))
    		return;
    	
    	if (name.equals("small") && mWikiCommonsImage.getThumbWidth() != 0 && 
    			mWikiCommonsImage.getThumbHeight() != 0 && mWikiCommonsImage.getFullHeight() == 0 
    			&& mWikiCommonsImage.getFullWidth() == 0) {
    		mSmallStarted = true;
    	}
    	
    	for (int i = 0; i < atts.getLength(); i++) {
			String attrName = atts.getQName(i);
			String value = atts.getValue(i);
		
			if (name.equals("div") && attrName.equals("class") && value.equals("fullImageLink")) {
	    		mGotImageClass = true;
	    		return;
			}
	    		
	    	if (mGotImageClass && !name.equals("a")) {
	    		mGotImageClass = false;
	    	} else if (mGotImageClass && name.equals("a") && attrName.equals("href") && 
	    			mWikiCommonsImage.getFullLink() == null) {
	    		mWikiCommonsImage.setFullLink(value.replace(' ', '_'));
	    		return;
	    	}
	    	
	    	if (mWikiCommonsImage.getFullLink() == null)
	    		return;
	    	
	    	if (name.equals("img") && attrName.equals("src") && mWikiCommonsImage.getThumbLink() == null) {
	    		//Log.v("Malw", "Got thumb link: " + value);
	    		Pattern pat = Pattern.compile("http");
				Matcher mat = pat.matcher(value);
				if (!mat.find()) {
					return;
				}
	    		mWikiCommonsImage.setThumbLink(value);
	    	}
	    	
	    	if (name.equals("img") && attrName.equals("width") && mWikiCommonsImage.getThumbWidth() == 0) {
	    		try {
	    			int width = Integer.parseInt(value);
		    		mWikiCommonsImage.setThumbWidth(width);
	    		} catch (NumberFormatException nfe) {
	    			mWikiCommonsImage.setThumbWidth(-1);
	    			return;
	    		}
	    	
	    	} 
	    	
	    	if (name.equals("img") && attrName.equals("height") && mWikiCommonsImage.getThumbHeight() == 0) {
	    		try {
	    			int height = Integer.parseInt(value);
	    			mWikiCommonsImage.setThumbHeight(height);
	    		} catch (NumberFormatException nfe) {
	    			mWikiCommonsImage.setThumbHeight(-1);
	    			return;
	    		}
	    	}
	    	
	    	
	    	if (name.equals("th") && attrName.equals("id") && value.equals("fileinfotpl_aut")) {
	    		mReadyForAuthor = 1;
	    	}
	    	
	    	if ((name.equals("a") || name.equals("p")) && mReadyForAuthor == 1) {
	    		mReadyForAuthor = 2;
	    	}
		}	
    }

    @Override
	public void characters (char ch[], int start, int length) {
    	if (!mReadyForFullRes && mReadyForAuthor != 2)
    		return;
    	
    	String line = "";
    	for (int i = start; i < start + length; i++) {
    		line += ch[i];    		
    	}
    	
    	if (mReadyForFullRes) {
    		Pattern pat = Pattern.compile("[(](\\d+,*\\d+).{3}(\\d+,*\\d+) pixels");
    		Matcher mat = pat.matcher(line);
    		String width = null;
    		String height = null;
    		if (!mat.find()) {
    			return;
    		}
    		
    		width = mat.group(1);
    		height = mat.group(2);
    		int index = width.indexOf(',');
    		if (index != -1)
   				width = width.substring(0, index) + width.substring(index + 1, width.length());

   			index = height.indexOf(',');
   			if (index != -1)
   				height = height.substring(0, index) + height.substring(index + 1, height.length());
    		try {
    			int w = Integer.parseInt(width);
    			int h = Integer.parseInt(height);
   				mWikiCommonsImage.setFullHeight(h);
   				mWikiCommonsImage.setFullWidth(w);
   			} catch (NumberFormatException nfe) {
   				mWikiCommonsImage.setFullHeight(-1);
   				mWikiCommonsImage.setFullWidth(-1);
    		}
    	
    		mReadyForFullRes = false;
    	} else if (mReadyForAuthor == 2) {
    		mWikiCommonsImage.setAuthor(line);
    	} 
    }

    @Override
	public void endElement (String uri, String name, String qName) {
    	if (mSmallStarted && name.equals("a")) {
    		mReadyForFullRes = true;
    		mSmallStarted = false;
    	} else if (mReadyForAuthor == 2 && name.equals("a")) {
    		mReadyForAuthor = 0;
    		mDone = true;
    	} else if (mReadyForAuthor == 2 && name.equals("p")) {
    		mReadyForAuthor = 0;
    		mDone = true;
    	}
    }

    @Override
	public void error (SAXParseException e) {
    }
    
    @Override
	public void fatalError (SAXParseException e) {
    }
    
    @Override
	public void warning (SAXParseException e) {
    }
    
    public WikiCommonsImage getWikiCommonsImage() {
    	return mWikiCommonsImage;
    }
}